#include "Debug.h"
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <strsafe.h>



void DEBUG_MSG_B(const char *szMsg, ... )
{
	UNREFERENCED_PARAMETER(szMsg);
	//return;
	va_list list;
	char buf[1024]={0},buf1[1024]={0};
	va_start(list,szMsg);
	vsprintf_s(buf,szMsg,list);
	va_end(list);
	sprintf_s(buf1,"%s%s%s","-->",buf,"\n");
	WriteFile(buf1);
}
void DEBUG_MSG_E(const char *szMsg, ... )
{
	UNREFERENCED_PARAMETER(szMsg);
	//return;
	va_list list;
	char buf[1024]={0},buf1[1024]={0};
	va_start(list,szMsg);
	vsprintf_s(buf,szMsg,list);
	va_end(list);
	sprintf_s(buf1,sizeof(buf1),"%s%s%s","<--",buf,"\n");
	WriteFile(buf1);

}
void DEBUG_MSG(const char *szMsg, ... )
{
	UNREFERENCED_PARAMETER(szMsg);
	va_list marker;
	char buf[1024]={0},buf1[1024]={0};
	memset(buf,0,1024);

	va_start( marker ,szMsg );  
	vsprintf_s(buf,szMsg, marker);
	va_end( marker );             
	sprintf_s(buf1,"%s%s",buf,"\n");
	WriteFile(buf1);
}

void WriteFile(LPSTR buf)
{
	FILE *file = NULL;
	errno_t err;
	err=fopen_s(&file,DEBUG_FILE_PATH, "a+" );
	if(err!=0)return;
	fseek(file,0,SEEK_END);
	fwrite(buf,strlen(buf),1,file);
	fclose(file);
}
void Log(
		 __in        LPSTR szInfo)
{
	CHAR szOutput [cchOUTPUT];

	if (SUCCEEDED(StringCchPrintf(
		szOutput,
		cchOUTPUT,
		"%s - %s\n",
		szOUTPUT_TAG,
		szInfo)))
		OutputDebugString(szOutput);
}

//
// Output a debug string and unsigned int
// 
void Log2(
		  __in        LPSTR szInfo,
		  __in        DWORD dwInfo)
{
	CHAR szOutput [cchOUTPUT];

	if (SUCCEEDED(StringCchPrintf(
		szOutput,
		cchOUTPUT,
		"%s - %s, %d\n",
		szOUTPUT_TAG,
		szInfo,
		dwInfo)))
		OutputDebugString(szOutput);
}


LPSTR GuidToString(const GUID &guid)
{
	char *buf=(char*)malloc(64);
sprintf_s(buf,64,"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
		guid.Data1, guid.Data2, guid.Data3,
		guid.Data4[0], guid.Data4[1],
		guid.Data4[2], guid.Data4[3],
		guid.Data4[4], guid.Data4[5],
		guid.Data4[6], guid.Data4[7]);
	return buf;
}